﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyHocPhi.Entity;


namespace QuanLyHocPhi.DAL
{
    public class CHUC_NANG_DAO
    {
        private const string SQL_SELECT_GROUPCHUC_NANGS_BYVAI_TRO = "select * from [CHUC_NANG] where MA_CHUC_NANG in (select MA_CHUC_NANG from VAITRO_CHUCNANG where TEN_CHUC_NANG like @Group and MA_VAI_TRO = @MA_VAI_TRO and TRANG_THAI <> 1)";

        // Parameters
        private const string PARM_GROUP = "@Group";
        private const string PARM_VAI_TRO_ID = "@MA_VAI_TRO";

        /// <summary>
        /// This method get CHUC_NANG base on MA_VAI_TRO and CHUC_NANG group
        /// </summary>
        /// <param name="group"></param>
        /// <param name="MA_VAI_TRO"></param>
        /// <returns></returns>
        public IList<CHUC_NANG> GetGroupCHUC_NANGsByVAI_TRO(string group, int MA_VAI_TRO)
        {
            IList<CHUC_NANG> CHUC_NANGs = new List<CHUC_NANG>();
            SqlParameter[] parms;
            parms = new SqlParameter[] {
											 new SqlParameter(PARM_GROUP, SqlDbType.NVarChar),
                                             new SqlParameter(PARM_VAI_TRO_ID, SqlDbType.Int)};
            parms[0].Value = group;
            parms[1].Value = MA_VAI_TRO;
            
            using (SqlDataReader reader = SQLDBHelper.ExecuteReader(SQLDBHelper.CONN_STRING,
                CommandType.Text, SQL_SELECT_GROUPCHUC_NANGS_BYVAI_TRO, parms))
            {
                while (reader.Read())
                {
                    CHUC_NANG CHUC_NANG = new CHUC_NANG();
                    CHUC_NANG.MA_CHUC_NANG = reader.GetInt32(reader.GetOrdinal("MA_CHUC_NANG"));
                    CHUC_NANG.TEN_CHUC_NANG = reader.GetString(reader.GetOrdinal("TEN_CHUC_NANG"));
                    if (!reader.IsDBNull(reader.GetOrdinal("Description")))
                        CHUC_NANG.MO_TA = reader.GetString(reader.GetOrdinal("Description"));
                    CHUC_NANGs.Add(CHUC_NANG);
                }
            }
            return CHUC_NANGs;
        }
    }
}
